<template>
  <!-- 顶部导航栏 -->
  <header class="navbar">
    <a href="#" class="logo" @click.prevent="toPath('/')">
      <svg xmlns="http://www.w3.org/2000/svg" fill="none" stroke="currentColor" viewBox="0 0 24 24">
        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
              d="M10 20l4-16m4 4l4 4-4 4M6 16l-4-4 4-4" />
      </svg>
      CodeHelp
    </a>

    <div class="nav-right">
      <nav class="nav-menu">
        <a href="#" @click.prevent="toPath('/publish')" v-if="data.isLogin">发布</a>
        <a href="#" @click.prevent="toPath('/my')" v-if="data.isLogin">我的</a>

        <a href="#" @click.prevent="toPath('/login')" v-if="!data.isLogin">登录</a>
      </nav>

      <!-- 用户头像下拉菜单 -->
      <div class="user-dropdown" id="userDropdown" v-if="data.isLogin">
        <div class="avatar">U</div>
        <div class="dropdown-menu">
          <a href="#" class="dropdown-item" @click.prevent="logout">退出登录</a>
        </div>
      </div>
    </div>
  </header>
</template>

<script setup>
import { onMounted, reactive } from "vue"
import router from '@/router'
import request from "@/utils/request"

function toPath(path) {
  router.push({ path })
}

onMounted(() => {
  const dropdown = document.getElementById('userDropdown')
  if (dropdown) {
    dropdown.addEventListener('click', function (e) {
      e.stopPropagation()
      this.classList.toggle('active')
    })

    document.addEventListener('click', () => {
      dropdown.classList.remove('active')
    })

    dropdown.querySelector('.dropdown-menu').addEventListener('click', e => {
      e.stopPropagation()
    })
  }
})

// 改为动态判断是否登录
const data = reactive({
  isLogin: !!localStorage.getItem('token') // 有 token 即认为已登录
})

// 监听路由变化更新登录状态（可选）
router.beforeEach(() => {
  data.isLogin = !!localStorage.getItem('token')
})

const logout = async () => {
  await request.post('/api/user/logout')
  localStorage.removeItem('token')
  data.isLogin = false
  toPath('/login')
}
</script>

<style scoped>
/* =============== 导航栏 =============== */
.navbar {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--white);
  box-shadow: 0 1px 6px rgba(0, 0, 0, 0.08);
  padding: 0 30px;
  height: 70px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  backdrop-filter: blur(8px);
}

.logo {
  font-size: 20px;
  font-weight: 700;
  color: var(--primary);
  text-decoration: none;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: color 0.2s;
}

.logo:hover {
  color: var(--primary-hover);
}

.logo svg {
  width: 26px;
  height: 26px;
}

.nav-right {
  display: flex;
  align-items: center;
  gap: 24px;
}

.nav-menu {
  display: flex;
  gap: 24px;
}

.nav-menu a {
  font-size: 15px;
  font-weight: 500;
  color: var(--text);
  text-decoration: none;
  transition: color 0.2s;
  position: relative;
}

.nav-menu a:hover {
  color: var(--primary);
}

.nav-menu a::after {
  content: '';
  position: absolute;
  bottom: -6px;
  left: 0;
  width: 0;
  height: 2px;
  background-color: var(--primary);
  transition: width 0.2s ease;
}

.nav-menu a:hover::after {
  width: 100%;
}

/* 用户头像 & 下拉菜单 */
.user-dropdown {
  position: relative;
  display: inline-block;
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--primary);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.2s;
}

.avatar:hover {
  transform: scale(1.05);
}

.dropdown-menu {
  display: none;
  position: absolute;
  right: 0;
  top: 50px;
  width: 100px;
  background: var(--white);
  border-radius: 10px;
  box-shadow: var(--shadow);
  overflow: hidden;
  z-index: 1000;
}

.dropdown-item {
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text);
  text-decoration: none;
  font-size: 14px;
  transition: background 0.2s;
}

.dropdown-item:hover {
  background: #f8fafc;
}

.dropdown-item svg {
  width: 16px;
  height: 16px;
  color: var(--text-light);
}

.user-dropdown.active .dropdown-menu {
  display: block;
}
</style>
